<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     Pagizer
 * @subpackage  backoffice
 */

class m_backoffice_extends_MenuNode
{
    private $document;
    private static $LABELWORDWRAP = 15;

    private $slide;
    private $type = 'parent';
    private $contextModule;
    private $lastParent = false;
    private $root = false;
    private $moduleName;
    private $translated;
    private $selectedId;
    private $valid;
    private $id;

    private function  __construct($document, $contextModule, $selectedId)
    {
        $this->document = $document;
        $this->contextModule = $contextModule;
        $this->selectedId = $selectedId;

        $contextUser = f_context_User::getInstance();
        $user = $contextUser->getUser();

        $this->valid['read'] = $user->hasDocRight($document, $contextModule, "read");

        if($this->valid['read'] == true)
        {
            $this->valid['edit'] = $contextUser->hasRight($contextModule."_Edit") && $user->hasDocRight($document, $contextModule, "edit");
            $this->valid['delete'] = $contextUser->hasRight($contextModule."_Delete") && $user->hasDocRight($document, $contextModule, "delete");
        }
    }

    /**
     * @param f_document_BaseDocument $document
     * @return m_backoffice_extends_MenuNode
     */
    public static function createNewFromDocument($document, $contextModule, $selectedId)
    {
        return new self($document, $contextModule, $selectedId);
    }

    /**
     * @param boolean $value
     * @return m_backoffice_extends_MenuNode
     */
    public function setSlide($value)
    {
        $this->slide = $value;
        return $this;
    }

    public function canSlide()
    {
        if(isset($this->slide) && !is_null($this->slide))
        {
            return $this->slide;
        }

        if(method_exists($this->document, 'canSlide'))
        {
            return $this->document->canSlide();
        }

        return true;
    }

    /**
     * @return m_backoffice_extends_MenuNode
     */
    public function setAsChild()
    {
        $this->type = 'child';
        return $this;
    }

    public function isChild()
    {
        return $this->type == 'child';
    }

    public function isParent()
    {
        return $this->type == 'parent';
    }

    public function isSelected()
    {
        return $this->selectedId == $this->document->getUniqueId();
    }

    public function isDisplayable()
    {
        return $this->valid['read'];
    }

    /**
     * Used to order modules list
     *
     * @param string $value
     * @return m_backoffice_extends_MenuNode
     */
    public function setId($value)
    {
        $this->id = $value;
        return $this;
    }

    public function getUniqueId()
    {
        if(!isset($this->id) || is_null($this->id))
        {
            return $this->document->getUniqueId();
        }
        return $this->id;
    }

    public function getLang()
    {
        return $this->document->getLang();
    }

    public function getIcon()
    {
        if(method_exists($this->document, 'getIcon'))
        {
            return $this->document->getIcon();
        }
        return 'menu_document.png';
    }

    public function isTargetable()
    {
        if(method_exists($this->document, 'isTargetable') && $this->valid['edit'])
        {
            return $this->document->isTargetable();
        }
        return false;
    }

    public function isTargeted()
    {
        if($this->isTargetable())
        {
            return $this->document->isTargeted();
        }
        return false;
    }

    public function getTargetEditLink()
    {
        if($this->isTargetable())
        {
            return $this->getLinkPrefix().f_core_Routing::getUrl("editTarget", array("moduleName" => $this->contextModule, "docId" => $this->document->getUniqueId(), "docLang" => $this->document->getLang()));
        }
        return '';
    }

    public function getStatus()
    {
        return method_exists($this->document, 'getPublicationStatusWithTemp') ? $this->document->getPublicationStatusWithTemp() : $this->document->getPublicationstatus();
    }

    public function getLabel()
    {
        $label = $this->requireTranslation() ? $this->document->getVODocument()->getLabel() : $this->document->getLabel();
        return f_utils_String::truncateAtLength($label, self::$LABELWORDWRAP);
    }

    public function getFullLabel()
    {
        return $this->requireTranslation() ? $this->document->getVODocument()->getLabel() : $this->document->getLabel();
    }

    public function getEditLink()
    {
        if(method_exists($this->document, 'getEditUrl'))
        {
            return $this->getLinkPrefix().$this->document->getEditUrl($this->document->getLang());
        }
        return '';
    }

    public function isDeletable()
    {
        if(method_exists($this->document, 'getDeleteUrl') && $this->valid['delete'])
        {
            return $this->document->getDeleteUrl($this->document->getLang()) != '';
        }
        return false;
    }

    public function getDeleteLink()
    {
        if(method_exists($this->document, 'getDeleteUrl'))
        {
            return $this->getLinkPrefix().$this->document->getDeleteUrl($this->document->getLang());
        }
        return '';
    }

    public function hasActions()
    {
        return ($this->isDeletable() || $this->isTargetable());
    }

    /**
     * @return m_backoffice_extends_MenuNode
     */
    public function setAsLastParent()
    {
        $this->lastParent = true;
        return $this;
    }

    public function isLastParent()
    {
        return $this->lastParent;
    }

    public function hasContextMenu()
    {
        if($this->isLastParent())
        {
            return count($this->getContextMenuItems()) > 0;
        }
        return false;
    }

    public function getContextMenuItems()
    {
        $contextMenuService = m_backoffice_services_ContextMenuService::getInstance();
        return $contextMenuService->getContextMenuItems($this->document);
    }

    /**
     *
     * @return m_backoffice_extends_MenuNode
     */
    public function setAsTranslated()
    {
        $this->translated = true;
        return $this;
    }

    /**
     *
     * @return m_backoffice_extends_MenuNode
     */
    public function setAsNotTranslated()
    {
        $this->translated = false;
        return $this;
    }


    public function requireTranslation()
    {
        if(isset($this->translated) && !is_null($this->translated))
        {
            return !$this->translated;
        }

        return !$this->document->isTranslated();
    }

    /**
     * @return m_backoffice_extends_MenuNode
     */
    public function setAsRoot()
    {
        $this->root = true;
        return $this;
    }

    public function isRoot()
    {
        return $this->root;
    }

    /**
     * @return m_backoffice_extends_MenuNode
     */
    public function setModuleName($value)
    {
        $this->moduleName = $value;
        return $this;
    }

    public function getModuleName()
    {
        return $this->moduleName;
    }

    public function isModuleRootNode()
    {
        return (isset($this->moduleName) && is_string($this->moduleName));
    }

    public function getNodeLinkClass()
    {
        $result = 'node ';

        if($this->isChild())
        {
            $result .= 'childNode ';
            $result .= $this->canSlide() ? 'more ':'noslide ';
        }
        else
        {
            $result .= 'expanded ';
            $result .= !$this->canSlide() ? 'noslide ' : '';
        }

        $result .= $this->isSelected() ? 'selected ' : '';

        return $result;
    }

    public function getContextModule()
    {
        return $this->contextModule;
    }

    private function getLinkPrefix()
    {
        return URL_RELATIVE.'/bo/#';
    }

    public function isAlias()
    {
        return $this->document->getModel() == 'modules_backoffice/alias';
    }

    public function getAliasEditLink()
    {
        return $this->getLinkPrefix().$this->document->getAliasEditUrl();
    }
}